<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.10.2.js"></script>
<!--<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.datepicker-zh-TW.js"></script>
-->
<script src="<?php echo URL::base() ?>media/laydate/laydate.js"></script>
<div class="container-fluid">
    <form class="form-horizontal" method='get' action='' name="form1" onsubmit="return checkForm();">
        <div class="control-group">
            <label class="control-label">出库时间:</label>
            <div class="controls">
                <input  class="input-medium datepicker" type="text"
                        value="<?php if (isset($getback['date_from'])) {
                            echo $getback['date_from'];
                        } ?>" name='date_from' id="date_from"/>
                到 <input  class="input-medium datepicker" type="text"
                          value="<?php if (isset($getback['date_to'])) {
                              echo $getback['date_to'];
                          } ?>" name='date_to' id="date_to"/>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">预约单号:</label>
            <div class="controls">
                <input id="search1"  type="text" name="purchaseid" 
                value="<?php if(!empty($getback['purchaseid'])){
                    echo $getback['purchaseid'];} ?>" />
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">状态:</label>
            <div class="controls">
                <select name="status">
                    <option value="all" <?php if($getback['status'] == 'all'){
                        echo "selected";
                    } ?> >请选择</option>
                    <option value="0" <?php if($getback['status'] == '0'){
                        echo "selected";
                    } ?>  >未打印</option>
                    <option value="1" <?php if($getback['status'] == '1'){
                        echo "selected";
                    } ?> >已打印</option>
                </select>
            </div>
        </div>
        <div class="control-group">
            <div class="controls">
                <input class="btn btn-info" type="submit" name="sub" value="查询"/>
            </div>
        </div>
    </form>
    <hr/>
    <div class="control-group">
        <?php if(!empty($result)){ ?>
        <input type="checkbox" id="check_all_start" value="">　
        <input  class="btn btn-success"  type='button'  id="printall" value='选中打印' />
        <?php } ?>
    </div>
    <?php foreach ($result as $k => $v) {
        ?>
    <table class="table table-hover " id="tabll<?php echo $k; ?>">
        <thead>
        <tr>
            <th width="2%"></th>
            <th width="5%">VMI单号</th>
            <th width="5%">质检单号</th>
            <th width="8%">SKU</th>
            <th width="12%">中文名称</th>
            <th width="10%">质检时间</th>
            <th width="5%">质检员</th>
            <th width="5%">状态</th>
            <th width="8%">凭证单号</th>
        </tr>

        </thead>
        <?php foreach ($v as $key => $value) {
         ?>
        <tr><td>
                <input class="tocheck" type="checkbox" name="<?php echo $value['purchaseorder_id']; ?>check[]" value="<?php echo $value['id']; ?>" ></td>
            <td><?php echo $value['purchaseorder_id'];?></td>
            <td><?php echo $value['id'];?></td>
            <td><?php echo $value['sku'];?></td>
            <td><?php echo $value['sku_name'];?></td>
            <td><?php echo $value['invoice_date'];?></td>
            <td><?php echo $users[$value['check_man']];?></td>
            <td><?php if($value['print_status'] == 1){
                echo "已打印";
            }else{
                echo "未打印";
            } ?></td>
            <td><?php echo $value['pur_evidence'];?></td>
        </tr>
        <?php } ?>
    </table>
    <div class="pull-right control-group">
        <input  class="btn btn-success"  type='button'  onclick="return print(<?php echo $value['purchaseorder_id']; ?>);" id="print<?php echo $value['purchaseorder_id']; ?>" value='打印' />
    </div>
    <?php } ?>
    <?php if(isset($page_links)){echo $page_links;}?>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        laydate.render({
            elem: '#date_from'
        });
        laydate.render({
            elem: '#date_to'
        });

    });
</script>

<script type="text/javascript">
    $(document).ready(function(){
        $("#check_all_start").click(function(){
//		var check = $("#check_all_start").prop("checked");
//		if(check){
//			$(".tocheck").prop("checked",true);
//		}else{
//			$(".tocheck").prop("checked",false);
//		}
            if(this.checked == true)
            {
                $(".tocheck").each(function(){
                    this.checked = true;
                });
            }
            else
            {
                $(".tocheck").each(function(){
                    this.checked = false;
                });
            }
        });
        $("#printall").click(function(){
            var mids=new Array();
            var all=new Array();
            var i=0;
            $(".tocheck").each(function(){
                if(this.checked==true){
                    var erid=$(this).val();
                    //$("#hidden"+erid).val(1);
                    var ername=$(this).attr('name');
                    var keys=ername.split('check[]');
                    var key=parseInt(keys[0]);
                    mids[key]='';
                    all[i]='';
                    i++;
                }
            });
            //alert(mids.length);return;
            var i=0;
            $(".tocheck").each(function(){
                if(this.checked==true){
                    var name=$(this).attr('name');
                    var keys=name.split('check[]');
                    var key=parseInt(keys[0]);
                    if(mids[key]!=''){
                        i--;
                        //var next=$(this).parent().parent().parent().parent().next().next().find('.tocheck');
                        //var next=$(this).parent().parent().next().find('td').find(".tocheck");
                        //var id=$(next).val();
                        //if($("#hidden"+id).val()==1){
                        //mids[i]+=','+$(this).val();
                        all[i]+=','+$(this).val();
                        //alert(mids[name]);
                        //alert(mids.length);return;
                        //}
                    }else{
                        //alert('else');
                        mids[key]+='0,'+$(this).val();
                        all[i]+='0,'+$(this).val();
                        //alert(mids.length);
                    }
                    i++;
                }
            });
            var total='';
            for(var j=0;j<all.length;j++){
                if(all[j]){
                    total+=(total=='')?all[j]:'+'+all[j];
                }else{
                    all[j]=0;
                }
            }
//		$.each(mids, function(i,val){
//			if(val!=''){
//			}
//		});
            var url = "<?php echo url::base(true)?>biaoqian/zhijianreports?id="+total;
            window.open(url);
        });
    });

    function checkForm(){
        var count = form1.purchaseid.value;
        var count = Number(count);
        if(isNaN(count)){
            alert('非法输入！');
            return false;
        }
        if(form1.date_from.value && !form1.date_to.value){
            alert('请选择结束时间！');
            return false;
        }
        if(form1.date_from.value > form1.date_to.value){

            alert('开始时间不能大于结束时间！');
            return false;
        }
        if(!form1.date_from.value && form1.date_to.value){
            alert('请选择开始时间！');
            return false;
        }
        

    }


    function print(id){
        var first=$("#hidden_first").val();
        var is_true = checkbox(id);
        if(!is_true){
            exit;
        }
        var checkid    = document.getElementsByName(id+'check[]');
        var mid   = 0;
        for(i=0;i<checkid.length;i++){

            if(checkid[i].checked == true){

                mid = mid +','+ checkid[i].value;
            }
        }
        //document.getElementById('tabll'+id).style.display = 'none';
        //document.getElementById('print'+id).style.display = 'none';
        var url = "<?php echo url::base(true)?>biaoqian/zhijianreport?id="+mid;
        //window.location.href=url;
        window.open(url);
        return false;

    }

    function checkbox(id)
    {
        var id = document.getElementsByName(id+'check[]');
        var isSel = false;
        for(i=0;i<id.length;i++){

            if(id[i].checked == true){

                isSel = true;
                return isSel;
            }
        }

        if(isSel == false)
        {
            alert('请选择质检单！');
            return false;
        }
    }

</script>